﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using University.Workshop.Business;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using System.Threading;
using System.Globalization;
using University.Workshop.DataAccess.LanguagePack.ClientLanguagePack;

namespace University.Workshop.ClientApp
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private string LanguageSelected;
        private string UserDontFound;
        private string UserIsPlaying;
        private string ErrorMessageUserNameNotSpaceOrNotEmpty;
        private string ErrorMessagePasswordShortOrNotValid;

        Validate validate = new Validate();
        public Client client = new Client();
        public Client hour = new Client();

        User user = new User();
        public Window1()
        {
            InitializeComponent();
            //string result = client.receive();

            //      client.Send("regular");
            //    string resulttwo = client.receive();

            this.LanguageSelected = "";
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(this.LanguageSelected);
            ApplyLanguage();
            ComboBoxInitializeComponent();
        }

        private void ApplyLanguage()
        {
            this.UserIsPlaying = ClientLanguagePack.LogInUserPlaying;
            this.UserDontFound = ClientLanguagePack.LogInUserDontFound;
            
            this.ErrorMessagePasswordShortOrNotValid = ClientLanguagePack.LogInErrorMessagePasswordShortOrNotValid;
            this.ErrorMessageUserNameNotSpaceOrNotEmpty = ClientLanguagePack.LogInErrorMessageUserNameNotSpaceOrNotEmpty;

            this.buttonLogIn.Content = ClientLanguagePack.LogInButtonLogIn;
            this.label_Language.Content = ClientLanguagePack.LogInLabelLanguage;
            this.labelRegister.Content = ClientLanguagePack.LogInLabelRegister;
            this.labelForgotPassword.Content = ClientLanguagePack.LogInLabelForgotPassowrd;
            this.label1.Content = ClientLanguagePack.LogInLabelServerHour;
            this.labelPassword.Content = ClientLanguagePack.LogInLabelPassword;
            this.labelUser.Content = ClientLanguagePack.LogInLabelUser;
        }

        private void buttonLogIn_Click(object sender, RoutedEventArgs e)
        {
            if (LogIn(textBoxUser.Text, passwordBox.Password))
            {

                windowStart start = new windowStart(user,this.LanguageSelected);
                start.Show();
                this.Close();
            }
        }
        public bool LogIn(string LogUser, string password)
        {
            try
            {
                client.Send("07 " + LogUser + " " + Convert.ToString((password).GetHashCode()));
                string resultfour = client.Receive();
                if (resultfour != "statusTrue")
                {
                    if (resultfour != "IncorrectPassword")
                    {
                        if (resultfour != "DoesnotFound")
                        {
                            if (resultfour.Split(' ')[1] != "are")
                            {
                                char space = ' ';
                                string[] text = resultfour.Split(space);
                                user.Name = text[0];
                                user.Answer = text[1];
                                user.Email = text[2];
                                user.Question = Convert.ToInt32(text[3]);
                                user.Password = text[4];
                                user.Chips = Convert.ToInt32(text[5]);
                                user.Status = Convert.ToInt32(text[6]);
                                user.UserLevel = Convert.ToInt32(text[7]);
                                user.TimeWon = Convert.ToInt64(text[8]);
                                return true;
                            }
                            else
                                MessageBox.Show(resultfour);
                            return false;
                        }
                        else
                        {
                            label3.Content = this.UserDontFound;
                            label3.Visibility = Visibility.Visible;
                            passwordBox.Password = "";
                            buttonLogIn.IsEnabled = false;
                            return false;
                        }
                    }
                    else
                    {
                        label3.Content = "Incorrect Password";
                        label3.Visibility = Visibility.Visible;
                        passwordBox.Password = "";
                        buttonLogIn.IsEnabled = false;
                        return false;
                    }
                }
                else
                {
                    label3.Content = this.UserIsPlaying;
                    label3.Visibility = Visibility.Visible;
                    passwordBox.Password = "";
                    buttonLogIn.IsEnabled = false;
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        private void textBoxUser_TextChanged(object sender, TextChangedEventArgs e)
        {
            bool uno = false;
            bool dos = false;
            string value = textBoxUser.Text;
            labelErrorUser.Content = " ";
            labelErrorUser.Visibility = Visibility.Visible;

            string res = validate.ValidateUserLength(value);
            if (res != " ")
            {
                labelErrorUser.Content = res;
                buttonLogIn.IsEnabled = false;
            }
            else
            {
                uno = true;
            }
            if (string.IsNullOrEmpty(value) || !validate.ValidateUserName(value,LanguageSelected))
            {
                labelErrorUser.Content = this.ErrorMessageUserNameNotSpaceOrNotEmpty;
                buttonLogIn.IsEnabled = false;
            }
            else
            {
                dos = true;
            }
            if (uno == true && dos == true && validate.ValidatePassword(passwordBox.Password))
            {
                buttonLogIn.IsEnabled = true;
            }

        }

        private void textBoxPassword_TextChanged(object sender, TextChangedEventArgs e)
        {

            string value = passwordBox.Password;
            labelErrorPassword.Content = " ";
            labelErrorPassword.Visibility = Visibility.Visible;
            if (!validate.ValidatePassword(value))
            {
                labelErrorPassword.Content = this.ErrorMessagePasswordShortOrNotValid;
                buttonLogIn.IsEnabled = false;
                //the key must contain numbers
            }
            else
            {
                if (validate.ValidateUserName(textBoxUser.Text,LanguageSelected))
                {
                    buttonLogIn.IsEnabled = true;
                }
            }
        }

        private void label1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            MainWindow register = new MainWindow(this.LanguageSelected);
            register.ShowDialog();
        }

        private void label1_MouseEnter(object sender, MouseEventArgs e)
        {
            labelRegister.Foreground = Brushes.Red;
            //rectangle1.Visibility = Visibility.Visible;
        }

        private void label1_MouseLeave(object sender, MouseEventArgs e)
        {
            labelRegister.Foreground = Brushes.SkyBlue;
            //rectangle1.Visibility = Visibility.Hidden;
        }

        private void label2_MouseEnter(object sender, MouseEventArgs e)
        {
            labelForgotPassword.Foreground = Brushes.Red;
            //rectangle2.Visibility = Visibility.Visible;
        }

        private void label2_MouseLeave(object sender, MouseEventArgs e)
        {
            labelForgotPassword.Foreground = Brushes.SkyBlue;
            //rectangle2.Visibility = Visibility.Hidden;
        }

        private void label2_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ForgotPassword forgotpassword = new ForgotPassword(this.LanguageSelected);
            forgotpassword.ShowDialog();
        }


        private void buttonLogInTwo_Click(object sender, RoutedEventArgs e)
        {
            PokerTable[] p = new PokerTable[5];

            /*for (int j = 0; j < 5; j++)
            {
                [j] = new PokerTable(2999, "user");
                p[j].Show();
            }*/



        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MainWindow register = new MainWindow(this.LanguageSelected);
            register.ShowDialog();

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void passwordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            string value = passwordBox.Password;
            labelErrorPassword.Content = " ";
            labelErrorPassword.Visibility = Visibility.Visible;
            if (!validate.ValidatePassword(value))
            {
                labelErrorPassword.Content = this.ErrorMessagePasswordShortOrNotValid;
                buttonLogIn.IsEnabled = false;
                //the key must contain numbers
            }
            else
            {
                if (validate.ValidateUserName(textBoxUser.Text,LanguageSelected))
                {
                    buttonLogIn.IsEnabled = true;
                }
            }
        }

        private void label1_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            hour.Send("71");
            MessageBox.Show(hour.Receive());
        }

        private void label1_MouseEnter_1(object sender, MouseEventArgs e)
        {
            label1.Foreground = Brushes.Red;
        }

        private void label1_MouseLeave_1(object sender, MouseEventArgs e)
        {
            label1.Foreground = Brushes.SkyBlue;
        }

        private void labelRegister_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MainWindow register = new MainWindow(this.LanguageSelected);
            register.ShowDialog();
        }

        private void labelForgotPassword_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ForgotPassword forgotpassword = new ForgotPassword(this.LanguageSelected);
            forgotpassword.ShowDialog();
        }

        private void comboBox_Language_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.comboBox_Language.SelectedIndex != -1)
            {
                this.LanguageSelected = this.comboBox_Language.SelectedItem.ToString();
                string[] AuxiliarLanguageSelected = this.LanguageSelected.Split(' ');
                this.LanguageSelected = AuxiliarLanguageSelected[1];
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(this.LanguageSelected);
                ApplyLanguage();
            }
        }

        private void ComboBoxInitializeComponent()
        {
            this.comboBox_Language.Items.Add("English EN-US");
            this.comboBox_Language.Items.Add("Español ES-ES");
            this.comboBox_Language.Items.Add("Italiano IT-IT");

            this.comboBox_Language.SelectedIndex = 0;
        }
    }
}
